/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Server.java
 *
 * Created on Nov 30, 2010, 11:27:10 AM
 */

package media_server;
import java.awt.*;
import java.io.*;
//import java.io.BufferedReader;
//import java.io.BufferedWriter;
//import java.io.File;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.io.OutputStream;
//import java.io.OutputStreamWriter;

import javax.swing.*;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import org.w3c.dom.Document;

class Connect{
    public  Document document=null;
//    public void TCPServer(String S_ip,String S_port1,String S_port2,String S_port3,String S_port4)
//    {
    public void TCPServer(String S_ip,String S_port1,String S_port2,String S_port3,String S_port4)
    {
        int KetNoi=0;
        String name;
        int check=0;
        String S_ip_Client="";
       try
        {
                ServerSocket s= new ServerSocket(3200);

                do
                {
                       Socket ss=s.accept();
                       //tạo một thread
                        Thread2 th = new Thread2(ss);
                }
                while (true);


        }
        catch(IOException e)
        {
                System.out.println("There're some error");
        }
    }


}
class conn
{
    public  Document document=null;
    public void th(Socket ss,String S_ip,String S_port1,String S_port2,String S_port3,String S_port4)
   {
        int KetNoi=0;
        String name="";
        int check=0;
        String S_ip_Client="";
        String S_ip_Server = "";
      //  Socket ss;

        S_ip_Client=ss.getRemoteSocketAddress().toString().replaceAll("/", "").split(":")[0];
        S_ip_Server = ss.getLocalAddress().toString().replaceAll("/", "").split(":")[0];
        
       try
       {
            InputStream is=ss.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(is));

            OutputStream os=ss.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            //KetNoi=2;
            KetNoi= CreateXmlFile_Client.kiemTraDuocPhepKetNoi(S_ip_Client);
            
            String receivedMessage;
               do{

                    receivedMessage=br.readLine();
                    if(receivedMessage.equals("quit"))
                        break;
                    if(receivedMessage.startsWith("name"))
                    {
                        if(check==1)
                        {
                            name = receivedMessage.split(":")[1];
                          //  CreateXmlFile_Client.CreateXmlFile_Client("Client.xml",document,S_ip_Client);
                           CreateXmlFile_Client.AddNodeDocument("Client.xml", document,S_ip_Client, name,"1", "1");
                           bw.write(1);
                           bw.newLine();
                           bw.flush();
                        }
                        else
                        {
                           CreateXmlFile_Client.AddNodeDocument_Attri_LogOut("Client.xml",document,name, S_ip_Client,"1", "1");
                           bw.write(1);
                           bw.newLine();
                           bw.flush();
                        }
                    }

                    if(receivedMessage.equals("request"))
                    {
                        if(KetNoi==1 || KetNoi==2)
                        {


                        /////////kiem tra client co duoc phep ket noi den server////////
                        //  chua ton tai, ko co ip => luu vao xml, ket noi
                        //  ton tai va duoc phep => cho ket noi
                        //  ton tai va khong duoc phep=> ko ket noi
                        ////////////////////////////////////////////////////////////////
                               bw.write(S_ip_Server+"/"+S_port1+"/"+S_port2+"/"+S_port3+"/"+S_port4);
                               bw.newLine();
                               bw.flush();
                               if(KetNoi==2)
                                   check=1;
                         }
                         else
                         {
                                if(KetNoi==0)
                                {
                                   bw.write("null");
                                   bw.newLine();
                                   bw.flush();
                                 }
                         }
                   }
                    if(receivedMessage.equals("logout"))
                    {
                       CreateXmlFile_Client.AddNodeDocument_Attri_LogOut("Client.xml",document,name, S_ip_Client,"0", "1");
                       bw.write(1);
                       bw.newLine();
                       bw.flush();
                    }
                 } while(true);
                    bw.close();
                    br.close();
       }
       catch (Exception e)
       {
       }

    }
}
class Thread2 implements Runnable
{
    Thread t;
    Socket ss;
    conn con=new conn();
    public Thread2(Socket s)
    {
        ss=s;
	t = new Thread(this);
	t.start();
    }
    public void run()
    {
        con.th(ss,Frm_IP.S_IP,"6000","7000","8000","9000");
    }
}
class threadTCPSever implements Runnable{
    Thread t;
    Socket socketserv;
    Connect util = new Connect();
    public threadTCPSever()
    {
	t = new Thread(this);
	t.start();
    }
    public void run()
    {
            util.TCPServer(Frm_IP.S_IP,"6000","7000","8000","9000");
    }
}

//---------
public class ServerMain extends javax.swing.JFrame {

    public static threadTCPSever client ;
   public int kenh1,kenh2,kenh3,kenh4;
   public static String ip;
    public  Document document=null;
    /** Creates new form Server */
    public ServerMain() {
         client = new threadTCPSever();
        kenh1=6000;
        kenh2=7000;
        kenh3=8000;
        kenh4=9000;
        initComponents();
         this.setTitle("Thieu Quang Lai: 0712233, Nguyen Ngoc Lam: 0712240, Vu Ngoc Hien: 0942037 - Class:TH2007/03, 09HCB");
         Dimension smh=Toolkit.getDefaultToolkit().getScreenSize();
        int w=this.getWidth();
        int h=this.getHeight();

        int x=(smh.width-w)/2;
         int y=(smh.height-h)/2;
         this.setLocation(x, y);
         //////Tao file client//
         CreateXmlFile_Client Client=new CreateXmlFile_Client();
         ///////////////////////
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLKenh1 = new javax.swing.JList();
        btKenh1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jLKenh2 = new javax.swing.JList();
        btKenh2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jLKenh3 = new javax.swing.JList();
        btKenh3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jLKenh4 = new javax.swing.JList();
        btKenh4 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        menu_dsClient = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel4.setBackground(new java.awt.Color(106, 222, 255));

        jScrollPane1.setViewportView(jLKenh1);

        btKenh1.setBackground(new java.awt.Color(106, 222, 255));
        btKenh1.setFont(new java.awt.Font("Tahoma", 1, 11));
        btKenh1.setText("Kênh 1");
        btKenh1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btKenh1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Danh sách:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(btKenh1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btKenh1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jToolBar1.setBackground(new java.awt.Color(0, 153, 204));
        jToolBar1.setRollover(true);

        jPanel5.setBackground(new java.awt.Color(106, 222, 255));

        jScrollPane2.setViewportView(jLKenh2);

        btKenh2.setBackground(new java.awt.Color(106, 222, 255));
        btKenh2.setFont(new java.awt.Font("Tahoma", 1, 11));
        btKenh2.setText("Kênh 2");
        btKenh2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btKenh2ActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Danh sách:");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(btKenh2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addContainerGap(141, Short.MAX_VALUE))))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btKenh2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel6.setBackground(new java.awt.Color(106, 222, 255));

        jScrollPane3.setViewportView(jLKenh3);

        btKenh3.setBackground(new java.awt.Color(106, 222, 255));
        btKenh3.setFont(new java.awt.Font("Tahoma", 1, 11));
        btKenh3.setText("Kênh 3");
        btKenh3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btKenh3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Danh sách:");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(btKenh3, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap(141, Short.MAX_VALUE))))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btKenh3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel7.setBackground(new java.awt.Color(106, 222, 255));

        jScrollPane4.setViewportView(jLKenh4);

        btKenh4.setBackground(new java.awt.Color(106, 222, 255));
        btKenh4.setFont(new java.awt.Font("Tahoma", 1, 11));
        btKenh4.setText("Kênh 4");
        btKenh4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btKenh4ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Danh sách:");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(btKenh4, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap(141, Short.MAX_VALUE))))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btKenh4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jMenuBar1.setBackground(new java.awt.Color(0, 153, 204));
        jMenuBar1.setForeground(new java.awt.Color(255, 255, 255));

        menu_dsClient.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        menu_dsClient.setForeground(new java.awt.Color(255, 255, 255));
        menu_dsClient.setText("Xem danh sách Client");
        menu_dsClient.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                menu_dsClientMouseClicked(evt);
            }
        });
        jMenuBar1.add(menu_dsClient);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 924, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    Channel1 mediaPanel1;Channel2 mediaPanel2;Channel3 mediaPanel3;Channel4 mediaPanel4; int ch1 = 0,ch2=0,ch3=0,ch4=0;
    private void btKenh1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btKenh1ActionPerformed
        // TODO add your handling code here:
        String[] dd = null;
        CreateXmlFile_Kenh1 xml_kenh1=new CreateXmlFile_Kenh1(Integer.toString(kenh1));// file Kenh1.xml
        if(ch1 == 1)
        {
            mediaPanel1.player.stop();
            mediaPanel1.av1.stop();
            mediaPanel1.dispose();
        }
        DefaultListModel listModel = new DefaultListModel();
        JFileChooser fileChooser1 = new JFileChooser();
        fileChooser1.setMultiSelectionEnabled(true);
        int result = fileChooser1.showOpenDialog( null );
        if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
        {
            File[] path = fileChooser1.getSelectedFiles();
            dd = new String[path.length];
            for(int i = 0; i<path.length; i++)
            {
                String item = path[i].getName();
                String a = i + 1 + ". " + item;
                
                dd[i] = path[i].getPath();
                listModel.addElement(a);
                 ///////////////// Ghi vao file XML đã có sẵn/////////////////////

                  CreateXmlFile_Kenh1.AddNodeDocument("Kenh1.xml",document,Integer.toString(kenh1),Integer.toString(i),listModel.get(i).toString().trim());


                    ////////////////////////////////
            }
            
            jLKenh1.setModel(listModel);

            //Select the new item and make it visible.
            jLKenh1.setSelectedIndex(0);
            jLKenh1.ensureIndexIsVisible(0);
            mediaPanel1 = new Channel1("kênh 1", dd, 0,Integer.toString(kenh1));
            ch1 = 1;
           // mediaPanel.setLocation(100, 100);
           // Dimension smh=Toolkit.getDefaultToolkit().getScreenSize();
          //  int w=mediaPanel.getWidth();
          //  int h=mediaPanel.getHeight();

           // int x=(smh.width-w)/2;
           //  int y=(smh.height-h)/2;
            // mediaPanel.setLocation(x, y);
        }
}//GEN-LAST:event_btKenh1ActionPerformed

    private void btKenh2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btKenh2ActionPerformed
        // TODO add your handling code here:
        String[] dd = null;
         CreateXmlFile_Kenh2 xml_kenh2=new CreateXmlFile_Kenh2(Integer.toString(kenh2));// file Kenh2.xml
          if(ch2 == 1)
        {
            mediaPanel2.player.stop();
            mediaPanel2.av2.stop();
            mediaPanel2.dispose();
        }
        DefaultListModel listModel = new DefaultListModel();
        JFileChooser fileChooser1 = new JFileChooser();
        fileChooser1.setMultiSelectionEnabled(true);
        int result = fileChooser1.showOpenDialog( null );
        if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
        {
            File[] path = fileChooser1.getSelectedFiles();
            dd = new String[path.length];
            for(int i = 0; i<path.length; i++)
            {
                String item = path[i].getName();
                String a = i + 1 + ". " + item;

                dd[i] = path[i].getPath();
                listModel.addElement(a);
                 ///////////////// Ghi vao file XML đã có sẵn/////////////////////

                  CreateXmlFile_Kenh2.AddNodeDocument("Kenh2.xml",document,Integer.toString(kenh2),Integer.toString(i),listModel.get(i).toString().trim());


                    ////////////////////////////////
            }

            jLKenh2.setModel(listModel);


            //Select the new item and make it visible.
            jLKenh2.setSelectedIndex(0);
            jLKenh2.ensureIndexIsVisible(0);
            mediaPanel2 = new Channel2("kênh 2", dd, 0,Integer.toString(kenh2));
            ch2 = 1;
          //  mediaPanel.setLocation(200, 100);
        }
    }//GEN-LAST:event_btKenh2ActionPerformed

    private void btKenh3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btKenh3ActionPerformed
        // TODO add your handling code here:
        String[] dd = null;
         CreateXmlFile_Kenh3 xml_kenh3=new CreateXmlFile_Kenh3(Integer.toString(kenh3));// file Kenh3.xml
         if(ch3 == 1)
        {
            mediaPanel3.player.stop();
            mediaPanel3.av3.stop();
            mediaPanel3.dispose();
        }
        DefaultListModel listModel = new DefaultListModel();
        JFileChooser fileChooser1 = new JFileChooser();
        fileChooser1.setMultiSelectionEnabled(true);
        int result = fileChooser1.showOpenDialog( null );
        if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
        {
            File[] path = fileChooser1.getSelectedFiles();
            dd = new String[path.length];
            for(int i = 0; i<path.length; i++)
            {
                String item = path[i].getName();
                String a = i + 1 + ". " + item;

                dd[i] = path[i].getPath();
                listModel.addElement(a);
                 ///////////////// Ghi vao file XML đã có sẵn/////////////////////

                  CreateXmlFile_Kenh3.AddNodeDocument("Kenh3.xml",document,Integer.toString(kenh3),Integer.toString(i),listModel.get(i).toString().trim());


                    ////////////////////////////////
            }

            jLKenh3.setModel(listModel);


            //Select the new item and make it visible.
            jLKenh3.setSelectedIndex(0);
            jLKenh3.ensureIndexIsVisible(0);
            mediaPanel3 = new Channel3("kênh 3", dd, 0,Integer.toString(kenh3));
            ch3 = 1;
           // mediaPanel.setLocation(100, 400);
        }
    }//GEN-LAST:event_btKenh3ActionPerformed

    private void btKenh4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btKenh4ActionPerformed
        String[] dd = null;
        CreateXmlFile_Kenh4 xml_kenh4=new CreateXmlFile_Kenh4(Integer.toString(kenh4));// file Kenh4.xml
        if(ch4 == 1)
        {
            mediaPanel4.player.stop();
            mediaPanel4.av4.stop();
            mediaPanel4.dispose();
        }
        DefaultListModel listModel = new DefaultListModel();
        JFileChooser fileChooser1 = new JFileChooser();
        fileChooser1.setMultiSelectionEnabled(true);
        int result = fileChooser1.showOpenDialog( null );
        if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
        {
            File[] path = fileChooser1.getSelectedFiles();
            dd = new String[path.length];
            for(int i = 0; i<path.length; i++)
            {
                String item = path[i].getName();
                String a = i + 1 + ". " + item;

                dd[i] = path[i].getPath();
                listModel.addElement(a);
                 ///////////////// Ghi vao file XML đã có sẵn/////////////////////

                  CreateXmlFile_Kenh3.AddNodeDocument("Kenh4.xml",document,Integer.toString(kenh4),Integer.toString(i),listModel.get(i).toString().trim());


                    ////////////////////////////////
            }

            jLKenh4.setModel(listModel);

            //Select the new item and make it visible.
            jLKenh4.setSelectedIndex(0);
            jLKenh4.ensureIndexIsVisible(0);
            mediaPanel4 = new Channel4("kênh 4", dd, 0,Integer.toString(kenh4));
            ch4 = 1;
           // mediaPanel.setLocation(400, 400);
        }
    }//GEN-LAST:event_btKenh4ActionPerformed

    private void menu_dsClientMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menu_dsClientMouseClicked
        // TODO add your handling code here:
        try {
            Frm_DsClient f=new Frm_DsClient();
            f.setVisible(true);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,"Lỗi: \n"+ex.getMessage());
        }
}//GEN-LAST:event_menu_dsClientMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ServerMain().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btKenh1;
    private javax.swing.JButton btKenh2;
    private javax.swing.JButton btKenh3;
    private javax.swing.JButton btKenh4;
    private javax.swing.JList jLKenh1;
    private javax.swing.JList jLKenh2;
    private javax.swing.JList jLKenh3;
    private javax.swing.JList jLKenh4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenu menu_dsClient;
    // End of variables declaration//GEN-END:variables

    

}
